package com.dscs;

import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.file.FileWriter;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Ebook {
    public static void main(String[] args) {
        try {
            List<String> chapterList = new ArrayList<String>();
            List<String> textList = new ArrayList<String>();
            List<Map<String, Object>> mapList = new ArrayList<>();
            FileInputStream fileInputStream = new FileInputStream(new File("C:\\Users\\13522\\Desktop\\xs\\zhuxian_xiaoding.txt"));
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8);
            BufferedReader novelbr = new BufferedReader(inputStreamReader);
            int currentIndex = 1;
            String line = novelbr.readLine();
            Map<String, Object> map = new HashMap<>();
            List<String> text = new ArrayList<>();
            String tip = "序章";
            while (line != null) {
                Pattern p1 = Pattern.compile("卷(一|二|三|四|五|六|七|八|九|十|百){1,9}");
                Matcher matcher1 = p1.matcher(line);
                if (matcher1.find()) {
                    tip = matcher1.group();
                }
                if (line.contains("第") && line.contains("章") && line.length() < 15) {
                    //结束上一章 存入mapList
                    map.put("context", text);
                    mapList.add(map);
                    //新一章开始
                    map = new HashMap<>();
                    map.put("tip", tip);
                    text = new ArrayList<>();
                    chapterList.add(line);
                    Pattern p = Pattern.compile("第(一|二|三|四|五|六|七|八|九|十|百){1,9}(章|回|篇|卷|集|部)\\s[\\u4e00-\\u9fa5]{0,}");
                    Matcher matcher = p.matcher(line);
                    if (matcher.find()) {
                        String titleName = matcher.group();
                        map.put("title", titleName);
                    }
                } else {
                    text.add(line);
                }
                line = novelbr.readLine();
            }
            map.put("context", text);
            mapList.add(map);
            for (Map<String, Object> s : mapList) {
                System.out.println(s.toString());
                File file = new File("C:\\Users\\13522\\Desktop\\xs\\" + (s.get("tip")==null?"序章":s.get("tip")) + "-" + (s.get("title")==null?"介绍":s.get("title")) + ".txt");
                try {
                    List<String> text2 = (List<String>) s.get("context");
                    //一行一行写入
                    for (String s1 : text2) {
                        cn.hutool.core.io.file.FileWriter writer = new FileWriter(file);
                        File file2 = writer.write(s1, true);
                        File file3 = writer.write("\r\n", true);
                    }
                } catch (IORuntimeException e) {
                    e.printStackTrace();
                }
//                ImageNumCode.createJieImage(tag++,(List<String>)s.get("context"));
            }
            novelbr.close();
            fileInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
